﻿/*
   Author: Tomer Paz
   games.com
   2007
*/
// the GameBoard Viewer (Optional Abstract class, for future enhanced implementations)
// responsible to update view once game board Data is changed.
// since game board contains Field views, the GameBoardView shall be responsible to create
// field views and attach each one to a Data field. Hence GameBoard is Fields Controller and Collection
// Builder class

package monopoly.views {
	import flash.display.Sprite;
	import flash.display.Shape;
	import fl.controls.Label;
	import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
	import flash.display.BlendMode;
	import flash.display.Loader;
	import monopoly.GameBoard.GameBoard;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import monopoly.IFS.iterators.IIterator;
	import monopoly.IFS.iterators.ArrayIterator;
	import monopoly.IFS.Collections.ICollection;
	import monopoly.IFS.GLOBALS;
	import monopoly.views.FieldView;
	import monopoly.Field.Field;

	
	public class GameBoardView extends Sprite implements ICollection
		{
		/* constants */	
		
		protected static const FIELD_DATA_SIZE = 150;
		
		/* inherited vars */
		
		protected var _Boarddata:GameBoard;
		protected var _FieldsViews:Array;
		protected var _Initiated:Boolean = false;
		protected var _bgLoader:Loader;
		
		
		public function GameBoardView(Data:GameBoard)
			{
			_Boarddata = Data;
			
			/* game board View is responsible for fields views.
			   Note! any child wishing to extend this class will surely want to expand functionality
			   over createFieldsViews() funcion (i.e. add functions), to affect fields alignment */

			/* listen to BoardData updates and redraw (applicable once BoardData finished loading ALL images) */
			
			_Boarddata.addEventListener(Event.CHANGE, draw);
			
			/* initiate data board bitmap load */
			
			_Boarddata.loadPropertyImages();
			}

		public function iterator(type:String = null):IIterator 
			{
	         return new ArrayIterator(_FieldsViews);
			}
			
		/* for each Data field in GambeBoard, create+attach a relevant FieldView */
		
		protected function createFieldsViews()
			{
			var BoardDIterator:IIterator   = _Boarddata.iterator();
			//var FldBitmapIterator:IIterator = _Boarddata.iterator("Bitmaps");
			
			_FieldsViews = new Array();
			
			while (BoardDIterator.hasNext())
				{
				var FieldData:Field = Field(BoardDIterator.next());
				
				/* set DataField size */
				
				FieldData.Size = FIELD_DATA_SIZE;
				
				/* push the Field View object into Container */
				
				_FieldsViews.push( new FieldView(FieldData) );	//, Loader(FldBitmapIterator.next())) );
				}
			}
			
		/* draw board */
		
		protected function draw(event:Event):void
			{
			this.blendMode = BlendMode.LAYER;
			
			/* if this is first time here, create the field views first */
			
			if (_FieldsViews == null)
				{
				if (GLOBALS.DEBUG & GLOBALS.DEBUG_VIEW)
					trace("[GameBoardView][draw] in if");
				// use opportunity since we know this is the first time we are here...
				
				/* TP 09/09 - add board background image, taken from DB */
			
				var rect:Shape = new Shape();
				rect.graphics.beginFill(0xFFFFFF);
				rect.graphics.drawRect(0, 0, _Boarddata.BoardWidth, _Boarddata.BoardHeight);
				rect.graphics.endFill();
				addChild(rect);
							
				_bgLoader = new Loader( );
				_bgLoader.mask = rect;
				_bgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onBoardImageComplete);
				_bgLoader.load(_Boarddata.GetBackground());
				_bgLoader.width = _Boarddata.BoardWidth;
				_bgLoader.height= _Boarddata.BoardHeight;
				}
			else
				{
				if (GLOBALS.DEBUG & GLOBALS.DEBUG_VIEW)
					trace("[GameBoardView][draw] in Else");
				// first draw a simple rectangle on this sprite
				// then add as childs all the field Views attached to this board
				graphics.beginFill( 0xffffff );
				graphics.drawRect(0, 0, _Boarddata.BoardWidth, _Boarddata.BoardHeight);
				graphics.endFill( );
				
				//the heavy part - hence not happens often - redraw all related Field views
				
				var FieldVIterator:IIterator = this.iterator();
				var BoardDIterator:IIterator = _Boarddata.iterator();
				
				/* this is where Board Level drawing logic shall be handled */
				
				while (FieldVIterator.hasNext())
					{
					var FieldData:Field = BoardDIterator.next() as Field;
					
					/* update field data - causes field redraw */
					
					/* add each field View as a Child of this Sprite if not already is */
					
					addChild( FieldVIterator.next() as FieldView );
					FieldData.Size = FIELD_DATA_SIZE;	// just for re-drawing FieldView
					}
	
				this.height = _Boarddata.BoardHeight;
				this.width	= _Boarddata.BoardWidth;
					
				/* to allow game controller to know when all images loaded and draw players
				   this is critical for GameBoardViewCollection to know when all boards loading is done */
				
				dispatchEvent(new Event(Event.COMPLETE));
				}
			}
			
		private function onBoardImageComplete(e:Event):void
			{
			addChild(_bgLoader.content);
			_bgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onBoardImageComplete);

			createFieldsViews();
			draw(null);
			}
		}
}